clear all
set more off
* set directory 

********************************************************************************
* TABLE 1 -- CHARACTERISTICS COMPARISON

* PANEL A: regressions of characteristics on ever treated indicator

{

* first collapse unit counts to decade to match census data
use analysis_file,clear

* apply sample restrictions
keep if never_ward==1 | ever_treated==1
drop if pop_flag==1
su mean_units_pc, det
drop if mean_units_pc > `r(p95)'

gen count=1
collapse (sum) total_units count, by(id decade)
rename decade year
rename total_units decade_units
rename count decade_observations
save decade_unit_counts, replace

* then decadal variables of interest
use analysis_file,clear

* apply sample restrictions
keep if never_ward==1 | ever_treated==1
drop if pop_flag==1
su mean_units_pc, det
drop if mean_units_pc > `r(p95)'

* keep census years
keep if inlist(year,1980,1990,2000,2010)==1
merge 1:1 id year using decade_unit_counts
keep if _merge==3
drop _merge

* drop years after treatments
keep if year<treatment_year

* create contemporaneous characteristics that match year of observation
gen pct_white=.
gen units_pc=.
gen pct_owned=.

local year_list 80 90 00 10
foreach year in `year_list'{

	replace pct_white = pct_white`year' if year==19`year' | year==20`year'
	replace units_pc = 1000*decade_units/pop`year' if year==19`year' | year==20`year'
	replace pct_owned = pct_owned`year' if year==19`year' | year==20`year'
	
}		

replace units_pc=units_pc/decade_observations
drop decade_observations

* run regressions on treatment indicators with different fixed effects
reg pop ever_treated i.year, vce(cl statefips)

areg pop ever_treated , absorb(state_year) vce(cl statefips)

reg pct_white ever_treated i.year, vce(cl statefips)

areg pct_white ever_treated , absorb(state_year) vce(cl statefips)

reg units_pc ever_treated i.year, vce(cl statefips)

areg units_pc ever_treated , absorb(state_year) vce(cl statefips)

reg pct_owned ever_treated i.year, vce(cl statefips)

areg pct_owned ever_treated , absorb(state_year) vce(cl statefips)

}

* PANEL B: change variables instead of level

{

use analysis_file,clear

* apply sample restrictions
keep if never_ward==1 | ever_treated==1
drop if pop_flag==1
su mean_units_pc, det
drop if mean_units_pc > `r(p95)'

* create growth variables and indicators for the treated decade
tsset id year
keep if inlist(year,1980,1990,2000,2010)==1
merge 1:1 id year using decade_unit_counts
keep if _merge==3
drop _merge
gen treated_decade=0
replace treated_decade=1 if treatment_year<year & treatment_year>year-10

gen lag_treated_decade=0
replace lag_treated_decade=1 if treatment_year<year-10 & treatment_year>year-20

gen lead_treated_decade=0
replace lead_treated_decade=1 if treatment_year>year & treatment_year<year+11

local var_list pop pct_owned pct_white

foreach var in `var_list'{

	gen `var'_growth80=(`var'80-`var'70)/`var'70
	gen `var'_growth90=(`var'90-`var'80)/`var'80
	gen `var'_growth00=(`var'00-`var'90)/`var'90
	gen `var'_growth10=(`var'10-`var'00)/`var'00

	gen `var'_growth=.
	replace `var'_growth=`var'_growth80 if year==1980
	replace `var'_growth=`var'_growth90 if year==1990
	replace `var'_growth=`var'_growth00 if year==2000
	replace `var'_growth=`var'_growth10 if year==2010
	
}

tsset id year

gen units_pc=.
local year_list 80 90 00 10
foreach year in `year_list'{

	replace units_pc = 1000*decade_units/pop`year' if year==19`year' | year==20`year'
	
}		

replace units_pc=units_pc/decade_observations
drop decade_observations

gen units_growth=(units_pc-L10.units_pc)/L10.units_pc
su units_growth, det
drop if units_growth > `r(p95)' & units_growth!=.

* run regressions (note you need to preserve then restore for Panel C regression below to run correctly)
preserve

keep if year<treatment_year

reg pop_growth ever_treated i.year, vce(cl statefips)

areg pop_growth ever_treated , absorb(state_year) vce(cl statefips)

reg pct_white_growth ever_treated i.year, vce(cl statefips)

areg pct_white_growth ever_treated , absorb(state_year) vce(cl statefips)

reg units_growth ever_treated i.year,  vce(cl statefips)

areg units_growth ever_treated , absorb(state_year)  vce(cl statefips)

reg pct_owned_growth ever_treated i.year,  vce(cl statefips)

areg pct_owned_growth ever_treated , absorb(state_year)  vce(cl statefips)

restore

}

* PANEL C: examining decade before switch specifically

{
	
* NOTE: the below regressions use the build steps from panel B.  thus, this do-file should be executed in one run

* relationship between changes and lead treated decade
reghdfe pop_growth lead_treated_decade if ever_treated==1 ,absorb(id year)  vce(cl statefips)

reghdfe pop_growth lead_treated_decade if ever_treated==1 ,absorb(id state_year)  vce(cl statefips)

reghdfe pct_white_growth lead_treated_decade if ever_treated==1 ,absorb(id year)  vce(cl statefips)

reghdfe pct_white_growth lead_treated_decade if ever_treated==1 ,absorb(id state_year)  vce(cl statefips)

reghdfe units_growth lead_treated_decade if ever_treated==1 ,absorb(id year)  vce(cl statefips)

reghdfe units_growth lead_treated_decade if ever_treated==1 ,absorb(id state_year)  vce(cl statefips)

reghdfe pct_owned_growth lead_treated_decade if ever_treated==1 ,absorb(id year)  vce(cl statefips)

reghdfe pct_owned_growth lead_treated_decade if ever_treated==1 ,absorb(id state_year)  vce(cl statefips)


}